<div th:replace="admin/module/_header"></div>
<div class="wrapper">
    <!-- 菜单栏模块 -->
    <div th:replace="admin/module/_sidebar"></div>
    <div class="content-wrapper">
        <style type="text/css" rel="stylesheet">
            .draft, .publish, .trash, .checking {
                list-style: none;
                float: left;
                margin: 0;
                padding-bottom: 10px
            }

            #btnNewPost {
                margin-left: 4px;
                padding: 3px 6px;
                position: relative;
                top: -4px;
                border: 1px solid #ccc;
                border-radius: 2px;
                background: #fff;
                text-shadow: none;
                font-weight: 600;
                font-size: 12px;
                line-height: normal;
                color: #3c8dbc;
                cursor: pointer;
                transition: all .2s ease-in-out
            }

            #btnNewPost:hover {
                background: #3c8dbc;
                color: #fff
            }

            .label {
                background-color: #d6cdcd;
            }

            .box-body {
                padding: 0;
            }

            .box {
                border-top: 0;
            }
        </style>
        <section class="content-header">
            <h1 style="display: inline-block;">文章管理</h1>
            <ol class="breadcrumb">
                <li>
                    <a data-pjax="true" href="/admin">
                        <i class="fa fa-dashboard"></i> 首页</a>
                </li>
                <li><a data-pjax="true" href="#">文章管理</a></li>
                <li class="active">文章列表</li>
            </ol>
        </section>
        <section class="content container-fluid">
            <div class="row">
                <div class="col-xs-12">
                    <ul style="list-style: none;padding-left: 0; vertical-align: center">
                        <li class="publish">
                            <a data-pjax="true" th:style="${status == 0 ? 'color: #000':''}" href="?status=0">已发布 |&nbsp;</a>
                        </li>
                        <li class="draft">
                            <a data-pjax="true" th:style="${status == 1 ? 'color: #000':''}" href="?status=1">草稿
                                |&nbsp;</a>
                        </li>
                        <li class="trash">
                            <a data-pjax="true" th:style="${status == 2 ? 'color: #000':''}" href="?status=2">回收站</a>
                        </li>
                    </ul>
                </div>
                <div class="col-xs-12">
                    <div class="col-md-4 col-xs-12" style="margin-bottom: 10px;padding-left: 0;">
                        <a class="btn btn-primary" href="/admin/post/new">
                            <i class="fa fa-plus"></i> 新增
                        </a>
                        <button type="button" class="btn btn-danger" onclick="batchDelete()">
                            <i class="fa fa-remove"></i> 批量删除
                        </button>
                    </div>
                    <div class="col-md-8 col-xs-12"
                         style="margin-bottom: 10px; text-align:right;padding-left: 0; padding-right: 0;">
                        <form class="form-inline" action="/admin/post">
                            <div class="form-group">
                                <input type="text" class="form-control" id="keywords" name="keywords"
                                       th:value="${keywords}" placeholder="请输入关键字">
                            </div>
                            <div class="form-group">
                                <select class="form-control" name="searchType">
                                    <option value="title" disabled>搜索范围</option>
                                    <option value="title" th:selected="${searchType == 'title'}">标题</option>
                                    <option value="content" th:selected="${searchType == 'content'}">内容</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <select class="form-control" name="status">
                                    <option value="0" disabled>状态</option>
                                    <option value="0" th:selected="${status == 0}">已发布</option>
                                    <option value="1" th:selected="${status == 1}">草稿</option>
                                    <option value="2" th:selected="${status == 2}">回收站</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <select class="form-control" name="sort">
                                    <option value="createTime" disabled>排序字段</option>
                                    <option value="createTime" th:selected="${sort == 'createTime'}">发布时间</option>
                                    <option value="postViews" th:selected="${sort == 'postViews'}">访问量</option>
                                    <option value="commentSize" th:selected="${sort == 'commentSize'}">回复数</option>
                                    <option value="postLikes" th:selected="${sort == 'postLikes'}">点赞数</option>
                                    <option value="isSticky" th:selected="${sort == 'isSticky'}">是否置顶</option>
                                    <option value="isRecommend" th:selected="${sort == 'isRecommend'}">是否推荐</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <select class="form-control" name="order">
                                    <option value="desc" disabled>升序/降序</option>
                                    <option value="asc" th:selected="${order == 'asc'}">升序</option>
                                    <option value="desc" th:selected="${order == 'desc'}">倒序</option>
                                </select>
                            </div>
                            <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> 搜索</button>
                        </form>
                    </div>
                </div>
                <div class="col-xs-12">
                    <div class="box box-primary">
                        <div class="box-body table-responsive">
                            <table class="table table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th><input type="checkbox" id="allSelect" onclick="doCheck()"></th>
                                    <th class="col-md-5">标题</th>
                                    <th>回复数</th>
                                    <th>点赞数</th>
                                    <th>访问量</th>
                                    <th>日期</th>
                                    <th class="col-md-3">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr th:id="'post-'+${post.id}" th:each="post : ${posts}">
                                    <td><input type="checkbox" name="ids" th:value="${post.id}"></td>
                                    <td>
                                        <label>
                                            <a th:href="'/post/'+${post.id}" target="_blank">
                                                <span th:if="${post.isSticky == 1}" class="font-success">【置顶】</span>
                                                <span th:if="${post.isRecommend == 1}" class="font-warning">【推荐】</span>
                                                [[${post.postTitle}]]
                                            </a>
                                        </label>
                                    </td>
                                    <td>
                                        <span class="label">[[${post.commentSize}]]</span>
                                    </td>
                                    <td>
                                        <span class="label">[[${post.postLikes}]]</span>
                                    </td>
                                    <td>
                                        <span class="label">[[${post.postViews}]]</span>
                                    </td>
                                    <td th:text="${#dates.format(post.createTime,'yyyy-MM-dd HH:mm')}"></td>

                                    <td th:if="${post.postStatus == 0}">
                                        <a th:href="'/post/'+${post.id}" class="btn btn-primary btn-xs"
                                           target="_blank">查看</a>
                                        <button class="btn btn-success btn-xs " th:if="${post.isSticky == 0}"
                                                th:onclick="'stickIt('+${post.id}+')'">置顶
                                        </button>
                                        <button class="btn btn-success btn-xs " th:if="${post.isSticky == 1}"
                                                th:onclick="'unStickIt('+${post.id}+')'">取消置顶
                                        </button>
                                        <button class="btn btn-warning btn-xs " th:if="${post.isRecommend == 0}"
                                                th:onclick="'recommendIt('+${post.id}+')'">推荐
                                        </button>
                                        <button class="btn btn-warning btn-xs " th:if="${post.isRecommend == 1}"
                                                th:onclick="'unRecommendIt('+${post.id}+')'">取消推荐
                                        </button>
                                        <a th:href="'/admin/post/edit?id='+${post.id}"
                                           class="btn btn-info btn-xs ">编辑</a>
                                        <button class="btn btn-danger btn-xs " th:onclick="'throwIt('+${post.id}+')'">
                                            丢弃
                                        </button>
                                    </td>
                                    <td th:if="${post.postStatus == 1}">
                                        <a th:href="'/admin/post/edit?id='+${post.id}" class="btn btn-info btn-xs ">编辑</a>
                                        <button class="btn btn-primary btn-xs " th:onclick="'revertIt('+${post.id}+')'">
                                            发布
                                        </button>
                                        <button class="btn btn-danger btn-xs " th:onclick="'throwIt('+${post.id+'}')">
                                            丢弃
                                        </button>
                                    </td>
                                    <td th:if="${post.postStatus == 2}">
                                        <button th:onclick="'revertIt('+${post.id}+')'" class="btn btn-primary btn-xs ">
                                            恢复
                                        </button>
                                        <button class="btn btn-danger btn-xs " th:onclick="'removeIt('+${post.id}+')'">
                                            删除
                                        </button>
                                    </td>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                        <div class="box-footer clearfix">
                            <div class="no-margin pull-left">
                                第 [[${pageInfo.current}]] / [[${pageInfo.pages}]] 页
                            </div>
                            <ul class="pagination no-margin pull-right">
                                <li><a data-pjax="true" th:classappend="${pageInfo.current == 1 ? 'disabled' : ''}"
                                       class="btn btn-sm"
                                       th:href="'/admin/post?status='+${status}+'&searchType='+${searchType}+'&keywords='+${keywords}+'&status='+${status}+'&sort='+${sort}+'&order='+${order}">
                                    首页
                                </a>
                                </li>
                                <li><a data-pjax="true" class="btn btn-sm"
                                       th:classappend="${pageInfo.current == 1 ? 'disabled' : ''}"
                                       th:href="'/admin/post?status='+${status}+'&page='+${pageInfo.current-1}+'&searchType='+${searchType}+'&keywords='+${keywords}+'&status='+${status}+'&sort='+${sort}+'&order='+${order}">
                                    上一页
                                </a>
                                </li>
                                <li><a data-pjax="true" class="btn btn-sm"
                                       th:classappend="${pageInfo.current == pageInfo.pages ? 'disabled' : ''}"
                                       th:href="'/admin/post?status='+${status}+'&page='+${pageInfo.current+1}+'&searchType='+${searchType}+'&keywords='+${keywords}+'&status='+${status}+'&sort='+${sort}+'&order='+${order}">
                                    下一页
                                </a>
                                </li>
                                <li><a data-pjax="true" class="btn btn-sm"
                                       th:classappend="${pageInfo.current == pageInfo.pages ? 'disabled' : ''}"
                                       th:href="'/admin/post?status='+${status}+'&page='+${pageInfo.pages}+'&searchType='+${searchType}+'&keywords='+${keywords}+'&status='+${status}+'&sort='+${sort}+'&order='+${order}">
                                    尾页
                                </a>
                                </li>
                            </ul>
                        </div>

                    </div>
                </div>
            </div>
        </section>
        <script>

            function revertIt(id) {
                $.ajax({
                    type: 'POST',
                    url: '/admin/post/revert',
                    async: false,
                    data: {
                        'id': id
                    },
                    success: function (data) {
                        if (data.code == 0) {
                            showMsg(data.msg, "error", 1000);
                        } else {
                            showMsgAndReload(data.msg, "success", 2000);

                        }
                    }
                });
            }

            function throwIt(id) {
                layer.confirm('请确认是否删除', function (index) {
                    $.ajax({
                        type: 'POST',
                        url: '/admin/post/throw',
                        async: false,
                        data: {
                            'id': id
                        },
                        success: function (data) {
                            if (data.code == 0) {
                                showMsg(data.msg, "error", 1000);
                            } else {
                                showMsgAndReload(data.msg, "success", 2000);
                            }
                        }
                    });
                    layer.close(index);
                });
            }

            function removeIt(id) {
                layer.confirm('请确认是否删除', function (index) {
                    $.ajax({
                        type: 'DELETE',
                        url: '/admin/post/delete',
                        async: false,
                        data: {
                            'id': id
                        },
                        success: function (data) {
                            if (data.code == 0) {
                                showMsg(data.msg, "error", 1000);
                            } else {
                                showMsgAndReload(data.msg, "success", 2000);
                            }
                        }
                    });
                    layer.close(index);
                });
            }


            function batchDelete() {
                var arr = [];
                $('input[name="ids"]:checked').each(function () {
                    arr.push($(this).val());
                });
                if (arr.length <= 0) {
                    showMsg('请选择需要删除的数据', "error", 1000);
                } else {
                    layer.confirm('请确认是否删除', function (index) {
                        $.ajax({
                            url: "/admin/post/batchDelete?ids=" + arr,
                            type: "DELETE",
                            success: function (data) {
                                if (data.code == 0) {
                                    showMsg(data.msg, "error", 1000);
                                } else {
                                    showMsgAndReload(data.msg, "success", 2000);
                                }
                            }
                        });
                        layer.close(index);
                    });
                }
            }


            function stickIt(id) {
                $.ajax({
                    type: 'POST',
                    url: '/admin/post/stick',
                    async: false,
                    data: {
                        'id': id
                    },
                    success: function (data) {
                        if (data.code == 0) {
                            showMsg(data.msg, "error", 1000);
                        } else {
                            showMsgAndReload(data.msg, "success", 2000);

                        }
                    }
                });
            }

            function unStickIt(id) {
                $.ajax({
                    type: 'POST',
                    url: '/admin/post/unStick',
                    async: false,
                    data: {
                        'id': id
                    },
                    success: function (data) {
                        if (data.code == 0) {
                            showMsg(data.msg, "error", 1000);
                        } else {
                            showMsgAndReload(data.msg, "success", 2000);

                        }
                    }
                });
            }

            function recommendIt(id) {
                $.ajax({
                    type: 'POST',
                    url: '/admin/post/recommend',
                    async: false,
                    data: {
                        'id': id
                    },
                    success: function (data) {
                        if (data.code == 0) {
                            showMsg(data.msg, "error", 1000);
                        } else {
                            showMsgAndReload(data.msg, "success", 2000);

                        }
                    }
                });
            }


            function unRecommendIt(id) {
                $.ajax({
                    type: 'POST',
                    url: '/admin/post/unRecommend',
                    async: false,
                    data: {
                        'id': id
                    },
                    success: function (data) {
                        if (data.code == 0) {
                            showMsg(data.msg, "error", 1000);
                        } else {
                            showMsgAndReload(data.msg, "success", 2000);

                        }
                    }
                });
            }
        </script>
    </div>
</div>
<div th:replace="admin/module/_footer"></div>